package com.doria.sso.service;

import com.doria.common.ad_enum.CodeEnum;
import com.doria.common.ad_enum.MsgEnum;
import com.doria.common.ad_enum.SMSCodeRedisKeyEnum;
import com.doria.sso.dto.LoginDto;
import com.doria.common.pojo.vo.HttpResult;
import com.doria.sso.service.interfaces.IAuthService;
import com.doria.sso.utils.JwtUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 安全相关业务层
 * @author Guochao
 * @see com.doria.sso.service.interfaces.IAuthService
 */
@Service
public class AuthServiceImpl implements IAuthService {

    private final RedisTemplate<String,String> redisTemplate;
    private final JwtUtils jwtUtils;

    public AuthServiceImpl(RedisTemplate<String, String> redisTemplate, JwtUtils jwtUtils) {
        this.redisTemplate = redisTemplate;
        this.jwtUtils = jwtUtils;
    }

    // 登录
    @Override
    public HttpResult login(LoginDto loginDto) {
        String loginCodeKey=SMSCodeRedisKeyEnum.LOGIN_CODE_KEY.getRedisKey()+loginDto.getMobile();
        String loginCode = redisTemplate.opsForValue().get(loginCodeKey);
        if (!loginDto.getSmsCode().equals(loginCode))
            return new HttpResult(false, CodeEnum.SMS_CODE_ERROR, MsgEnum.SMS_CODE_ERROR);
        return jwtUtils.getToken(loginDto.getMobile());
    }
}
